package com.lanou.sinasports.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.sinasports.R;
import com.lanou.sinasports.activity.SpecailDecActivity;
import com.lanou.sinasports.beans.Special;
import com.lanou.sinasports.utils.BitmapCache;

import java.util.List;

/**
 * Created by dllo on 16/1/16.
 */
public class SpecialAdapter extends BaseAdapter {
    private Context context;
    private List<Special> data;
    private RequestQueue queue;
    private ImageLoader imageLoader;

    public SpecialAdapter(Context context, List<Special> data) {
        this.context = context;
        this.data = data;
        queue= Volley.newRequestQueue(context);
        imageLoader=new ImageLoader(queue,new BitmapCache());
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        SpecViewHolder specViewHolder;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.item_special,parent,false);
            specViewHolder=new SpecViewHolder(convertView);
            convertView.setTag(specViewHolder);
        }else {
            specViewHolder= (SpecViewHolder) convertView.getTag();
        }
           specViewHolder.tvTitle.setText(data.get(position).getTitle());
           specViewHolder.tvDesc.setText(data.get(position).getDesc());

           specViewHolder.imageView.setDefaultImageResId(R.mipmap.ic_main_tab_special_p);
           specViewHolder.imageView.setErrorImageResId(R.mipmap.ic_main_tab_team_p);
           specViewHolder.imageView.setImageUrl(data.get(position).getPic(), imageLoader);

           specViewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   Intent intent=new Intent(context, SpecailDecActivity.class);
                   intent.putExtra("id",data.get(position).getId());
                   intent.putExtra("num",data.get(position).getNum()+"");
                   context.startActivity(intent);
               }
           });

        return convertView;
    }

    class SpecViewHolder{
        NetworkImageView imageView;
        TextView  tvTitle;
        TextView  tvDesc;
        LinearLayout linearLayout;
        public SpecViewHolder(View view) {
            imageView= (NetworkImageView) view.findViewById(R.id.iv_special_pic);
            tvTitle= (TextView) view.findViewById(R.id.tv_item_special_title);
            tvDesc= (TextView) view.findViewById(R.id.tv_special_desc);
            linearLayout= (LinearLayout) view.findViewById(R.id.ll_item_special);
        }
    }
}
